<?php
//imports
require_once("User.php");
	
class Expense_DAO{
	//Variables
		private $id;
		private $title;
		private $description;
		private $amount;
		private $currency;
		private $User_id;
		

	//Accessors
		
	public function getId(){
		return $this->id;
	}
	
	public function setId($p_id){
		$this->id=$p_id;
	}
		
	public function getTitle(){
		return $this->title;
	}
	
	public function setTitle($p_title){
		$this->title=$p_title;
	}
		
	public function getDescription(){
		return $this->description;
	}
	
	public function setDescription($p_description){
		$this->description=$p_description;
	}
		
	public function getAmount(){
		return $this->amount;
	}
	
	public function setAmount($p_amount){
		$this->amount=$p_amount;
	}
		
	public function getCurrency(){
		return $this->currency;
	}
	
	public function setCurrency($p_currency){
		$this->currency=$p_currency;
	}
		
	public function getUser_id(){
		return $this->User_id;
	}
	
	public function setUser_id($p_User_id){
		$this->User_id=$p_User_id;
	}
		
	
	//Accessors for foreignKeys
		public function getUser_id_Ref(){
		return User::getFromPrimaryKey($this->getUser_id());
	}
		
	
	//Method to get referenced objects
		
	/**
	 * Attempt an INSERT if the primary key is empty, otherwise perform an UPDATE.
	 * @param boolean $p_forceCreation true if save must trigger a create (by-pass the automatic ID generation) 
	 */
	public function save($p_forceCreation=false){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			
						$l_hasKey=$this->getId();
			if(!isset($l_hasKey) || $p_forceCreation){
				//Attempt an INSERT
				if(!$p_forceCreation){
					$this->setId(strval(uniqid("W-", true)) . "-" . strval(rand(100000,999999)));
				}
								$l_sth = $l_dbh->prepare("INSERT INTO Expense (id,title,description,amount,currency,User_id) VALUES (:id,:title,:description,:amount,:currency,:User_id)");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':title',$this->getTitle());
									$l_sth->bindParam(':description',$this->getDescription());
									$l_sth->bindParam(':amount',$this->getAmount());
									$l_sth->bindParam(':currency',$this->getCurrency());
									$l_sth->bindParam(':User_id',$this->getUser_id());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}
			}else{
				//Attempt an UPDATE
								$l_sth = $l_dbh->prepare("UPDATE Expense SET title=:title, description=:description, amount=:amount, currency=:currency, User_id=:User_id WHERE id=:id");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':title',$this->getTitle());
									$l_sth->bindParam(':description',$this->getDescription());
									$l_sth->bindParam(':amount',$this->getAmount());
									$l_sth->bindParam(':currency',$this->getCurrency());
									$l_sth->bindParam(':User_id',$this->getUser_id());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}				
			}
			
			$l_dbh = null;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Delete the current record from the database
	 */
	public function delete(){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
						$l_sth = $l_dbh->prepare("DELETE FROM Expense WHERE id=:value");
			$l_sth->bindParam(':value', $this->getId());
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			//Set the primaryKey to null so that the object might be saved again (=created again)
			$this->setId(null);
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}	
	}
	
	
	/**
	 * Return all the objects from the table
	 */
	public static function getAll(){
		try {
			$l_results=array();
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			foreach ($l_dbh->query("SELECT * FROM Expense") as $l_row) {
				$l_object=new Expense();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setAmount($l_row['amount']);
								$l_object->setCurrency($l_row['currency']);
								$l_object->setUser_id($l_row['User_id']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the object identified by the specified key
	 * @param unknown_type $p_id the primary key value
	 */
	public static function getFromPrimaryKey($p_id){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM Expense WHERE id=:value");
			$l_sth->bindParam(':value', $p_id);
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			if($l_row){
				$l_object=new Expense();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setAmount($l_row['amount']);
								$l_object->setCurrency($l_row['currency']);
								$l_object->setUser_id($l_row['User_id']);
							}
			$l_dbh = null;
			return $l_object;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getFromCustomCriteria($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM Expense {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_rows=$l_sth->fetchAll(PDO::FETCH_ASSOC);
			$l_results=array();
			
			foreach($l_rows as $l_row) {
				$l_object=new Expense();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setAmount($l_row['amount']);
								$l_object->setCurrency($l_row['currency']);
								$l_object->setUser_id($l_row['User_id']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getCustomCount($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT COUNT(*) as count FROM Expense {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			$l_dbh = null;
			if($l_row){
				return $l_row["count"];
			}else{
				die();
			}
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects<br>
	 */
	public static function getCount(){
		return Expense::getCustomCount("",array()); 
	}
}
?>